package com.cloudminga.gui;

import com.globussoft.amazons3.AmazonS3Utility;
import com.globussoft.xml.SettingsXml;
//import com.pomblast.persistentlayer.User;
import com.cloudminga.database.DataBaseHelper;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Gui_1.java
 *
 * Created on Dec 17, 2009, 11:50:59 AM
 */
/**
 *
 * @author jyoti
 */
public class Config_1_login extends javax.swing.JDialog {

    public static String username;
    public static String pass;
    DataBaseHelper databaseHelperObject;
    AmazonS3Utility amazonS3UtilityObject;
    private String projectName="";


    /** Creates new form Gui_1 */
    public String getUserName()
    {
        return username;
    }
    public Config_1_login(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        projectName=SettingsXml.getProjectName();
        databaseHelperObject = new DataBaseHelper();
        amazonS3UtilityObject= new AmazonS3Utility();
        Dimension dimension = new Dimension(600, 500);
        initComponents();
        this.setTitle("Welcome to "+projectName+" Backup Wizard Configuration");
        setSize(600, 500);
        Toolkit toolkit = Toolkit.getDefaultToolkit();
        int x = (toolkit.getScreenSize().width - getWidth()) / 2;
        int y = (toolkit.getScreenSize().height - getHeight()) / 2;
        setResizable(true);
        setLocation(x, y);
        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        // setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
      

        String s = "<html>"
                + "<body>"
                + "<font size=3 color=black>"
                + "Please enter your account"
                + "<br>"
                + "information. If you have not<br>created account, click the"
                + "<br>"
                + "signup link."
                + "<br>"
                + "</body>"
                + "</html>";
        l1.setText(s);
        String s2 = "<html><body><font size=2><a href=>Sign up now</a></body></html>";
        label_signup.setText(s2);

        try {
            /* code to set the look and feel according to the native operating system */

            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            SwingUtilities.updateComponentTreeUI(this);

        } catch (Exception e) {
            System.out.println("Problem in setting look and feel");
        }
        lable_logo.setOpaque(false);
    }

    Config_1_login() {
        // throw new UnsupportedOperationException("Not yet implemented");
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        nextLoginButton = new javax.swing.JButton();
        cancelLoginButton = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        text_Email = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        Text_Password = new javax.swing.JPasswordField();
        l1 = new javax.swing.JLabel();
        label_signup = new javax.swing.JLabel();
        checkbox_pwd = new javax.swing.JCheckBox();
        image_label = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        lable_logo = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("");
        setForeground(java.awt.Color.white);
        setMinimumSize(new java.awt.Dimension(600, 500));
        setModal(true);
        setName("login"); // NOI18N
        setResizable(false);
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        nextLoginButton.setText("Next ");
        nextLoginButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                nextButtonClickActionPerformed(evt);
            }
        });
        getContentPane().add(nextLoginButton, new org.netbeans.lib.awtextra.AbsoluteConstraints(360, 410, -1, -1));

        cancelLoginButton.setText("Cancel");
        cancelLoginButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelLoginButtonActionPerformed(evt);
            }
        });
        getContentPane().add(cancelLoginButton, new org.netbeans.lib.awtextra.AbsoluteConstraints(450, 410, -1, -1));

        jLabel1.setText("Username: (E-mail Address)");
        getContentPane().add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(340, 180, 160, -1));
        getContentPane().add(text_Email, new org.netbeans.lib.awtextra.AbsoluteConstraints(340, 200, 150, -1));

        jLabel2.setText("password:");
        getContentPane().add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(340, 230, 130, -1));

        Text_Password.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                Text_PasswordKeyPressed(evt);
            }
        });
        getContentPane().add(Text_Password, new org.netbeans.lib.awtextra.AbsoluteConstraints(340, 250, 150, -1));

        l1.setBackground(new java.awt.Color(255, 255, 255));
        l1.setText("please enter account inf");
        getContentPane().add(l1, new org.netbeans.lib.awtextra.AbsoluteConstraints(340, 100, 210, 80));

        label_signup.setText("signup");
        getContentPane().add(label_signup, new org.netbeans.lib.awtextra.AbsoluteConstraints(350, 350, 100, -1));

        checkbox_pwd.setText("Remember my password");
        checkbox_pwd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                checkbox_pwdActionPerformed(evt);
            }
        });
        getContentPane().add(checkbox_pwd, new org.netbeans.lib.awtextra.AbsoluteConstraints(340, 300, 160, -1));

        image_label.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/config_1_login.jpg"))); // NOI18N
        getContentPane().add(image_label, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 90, 250, 260));

        jPanel1.setBackground(new java.awt.Color(204, 204, 204));
        jPanel1.setForeground(new java.awt.Color(153, 153, 153));
        jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        lable_logo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/logo_2.png"))); // NOI18N
        jPanel1.add(lable_logo, new org.netbeans.lib.awtextra.AbsoluteConstraints(520, 10, -1, 50));

        jLabel3.setFont(new java.awt.Font("Arial", 0, 18));
        jLabel3.setForeground(new java.awt.Color(51, 51, 255));
        jLabel3.setText("Login");
        jPanel1.add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 10, -1, -1));

        jLabel4.setFont(new java.awt.Font("Arial", 0, 12));
        jLabel4.setForeground(new java.awt.Color(51, 51, 255));
        jLabel4.setText("Let's get Started");
        jPanel1.add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(50, 40, -1, -1));

        getContentPane().add(jPanel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 580, 70));

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void nextButtonClickActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nextButtonClickActionPerformed
     insert();
}//GEN-LAST:event_nextButtonClickActionPerformed

    private void checkbox_pwdActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_checkbox_pwdActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_checkbox_pwdActionPerformed

    private void cancelLoginButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelLoginButtonActionPerformed
      this.dispose();
    }//GEN-LAST:event_cancelLoginButtonActionPerformed

    private void Text_PasswordKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_Text_PasswordKeyPressed
           if(!(text_Email.getText().equals("")&&Text_Password.getText().equals("")))
           {
               if(evt.getKeyCode()==10)
               insert();
           }
    }//GEN-LAST:event_Text_PasswordKeyPressed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                Config_1_login dialog = new Config_1_login(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    private void insert()
    {
        boolean rememberpass = false;
        username = text_Email.getText();
        char[] password = Text_Password.getPassword();
        pass = new String(password);
        //Set the email pattern string
     //  Pattern p = Pattern.compile(".+@.+\\.[a-z]+");
        Pattern p = Pattern.compile("^[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$");
        //Match the given string with the pattern
        Matcher m = p.matcher(username);

        //check whether match is found
        boolean matchFound = m.matches();
        if (checkbox_pwd.isSelected() == true) {
            rememberpass = true;
        }
        if (username.isEmpty() || pass.isEmpty() || matchFound != true) {
            JOptionPane.showMessageDialog(null,
                    "Enter UserName or PassWord.",
                    "Error Message",
                    JOptionPane.ERROR_MESSAGE);
        } else if(pass.length()<6)
        {
          JOptionPane.showMessageDialog(null,
                    "Password Must Be Atleast 6 Digit",
                    "Error Message",
                    JOptionPane.ERROR_MESSAGE);

        }
        else if (databaseHelperObject.check_login(username, pass)==false) {//||amazonS3UtilityObject.checkAvaibelityUserName(username.replace("@", "."))==false
            JOptionPane.showMessageDialog(null,
                    "User Id Does Not Exist",
                    "Error Message",
                    JOptionPane.ERROR_MESSAGE);
        }
        else {
            SettingsXml setting = new SettingsXml();
            setting.setUser(username);
            setting.setPassword(pass);
            setting.setRememberPass(rememberpass);
            //User conlog=  new User();
            // conlog.ConfigureLogin(username,pass,rememberpass);
            this.dispose();
            new Config_2_setting(new javax.swing.JFrame(), true).setVisible(true);
        }
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    public javax.swing.JPasswordField Text_Password;
    private javax.swing.JButton cancelLoginButton;
    private javax.swing.JCheckBox checkbox_pwd;
    private javax.swing.JLabel image_label;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JLabel l1;
    private javax.swing.JLabel label_signup;
    private javax.swing.JLabel lable_logo;
    private javax.swing.JButton nextLoginButton;
    public javax.swing.JTextField text_Email;
    // End of variables declaration//GEN-END:variables
}
